Imports ModuloAdministraciondeEnvios.Entity
Imports ModuloAdministraciondeEnvios.BusinessLogicLayer
Imports ModuloAdministraciondeEnvios.DataAccessLayer
Imports System.Data.SqlClient
Imports CrystalDecisions



Namespace PresentationLayer
    Public Class FrmEnvios

        Private Sub FrmEnvios_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

            TraerEnviosaGrid()
            Me.LabelOrigenNombre.Text = NombreOrigen
            Me.LabelOrigenDomicilio.Text = DomicilioOrigen
            Me.LabelOrigenCUIT.Text = CUITOrigen
            Me.LabelOrigenLocalidad.Text = LocalidadOrigen
            Me.LabelOrigenCP.Text = CPOrigen
            Me.TextBoxOrigenID.Text = IdOrigen

            Me.LabelDestino.Text = NombreDestino
            Me.LabelDestinoDomicilio.Text = DomicilioDestino
            Me.LabelDestinoCUIT.Text = CUITDestino
            Me.LabelDestinoLocalidad.Text = LocalidadDestino
            Me.LabelDestinoCP.Text = CPDestino
            Me.TextBoxDestinoID.Text = IdDestino

        End Sub

        Private Sub FrmEnvios_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
      
        End Sub


        Private Sub FrmEnvios_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            DataGridViewEnvios.RowHeadersVisible = False
            DataGridViewEnvios.AllowUserToAddRows = False
            DataGridViewEnvios.AllowUserToOrderColumns = False

            Me.DataGridViewEnvios.AutoGenerateColumns = True

            DataGridViewEnvios.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

            DataGridViewEnvios.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
            DateTimePickerFecha.Value = Now.Date

            MaskedTextBox1.Mask = "00/00/00"
            MaskedTextBox1.Text = Format(DateTimePickerFecha.Value.Date, "ddMMyy")


            RadioButtonExpreso.Checked = True
            RadioButtonPagoOrigen.Checked = True

            TextBoxImporteContrareembolso.Enabled = False
            TextBoxImporteContrareembolso.Text = 0.0
            ContextMenuEnvios.ShowImageMargin = False

            ContextMenuEnvios.Items.Add("Borrar", Nothing, New System.EventHandler(AddressOf BorrarEnvio))
            ContextMenuEnvios.Items.Add("Editar y Modificar", Nothing, New System.EventHandler(AddressOf EditarEnvio))

            DataGridViewEnvios.ContextMenuStrip = ContextMenuEnvios

            TraerMovilesTodos()

            Dim n As New DataEnvio

            'n.IdEnvio = 5
            'n.Remitente = 1
            'n.Destinatario = 3
            'n.Fecha = Now().Date
            'n.CantidadDePaquetes = 2
            'n.ImporteContrareembolso = 11.95
            'n.ImporteEnvio = 14.89
            'n.Movil = 1
            'n.TipoDeEnvio = 1
            'n.TipoDePago = 1
            'n.UltimoEstado = 1

            'Dim Grabar As IEnvio = DirectCast(n, IEnvio)
            'Dim Resultado As Boolean = Grabar.AgregarEnvio(n)

            TraerEnviosaGrid()


            'Dim grab As IEnvio = DirectCast(n, IEnvio)
            'Dim resultado1 As Boolean = grab.AgregarEnvio(n)

            'Me.ReportViewer1.RefreshReport()
        End Sub



        Public Sub TraerEnviosaGrid()

            Try
                Me.DataGridViewEnvios.DataSource = ""
                Me.DataGridViewEnvios.Columns.Remove("Detalle")
            Catch ex As Exception

            End Try

            Dim n1 As New DataEnvio
            Dim TraerDatos As IEnvio = DirectCast(n1, IEnvio)
            Datos = TraerDatos.TraerEnviosCompletosPorFechas("01/01/10", "31/12/10", 2)
            Me.DataGridViewEnvios.DataSource = Datos.Tables(0)

            Dim Columna1 As New DataGridViewButtonColumn
            Columna1.HeaderText = "Cantidad de Paquetes"
            Columna1.Name = "Detalle"
            Me.DataGridViewEnvios.Columns.Insert(1, Columna1)

            For Each row As DataGridViewRow In Me.DataGridViewEnvios.Rows
                row.Cells(1).Value = row.Cells(15).Value
            Next

            'For i As Integer = 0 To Me.DataGridViewEnvios.Rows.Count - 1
            'Me.DataGridViewEnvios.Rows(i).Cells(1).Value = "2342"  ' Me.DataGridViewEnvios.Rows(i).Cells(15).Value
            'MsgBox(i)
            'Next

            Me.DataGridViewEnvios.AutoResizeColumns()
            Me.DataGridViewEnvios.Update()

        End Sub



        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim BuscarOrigen As New FrmBuscaOrigen
            BuscarOrigen.ShowDialog()

        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim BuscarDestinatario As New FrmBuscaDestino
            BuscarDestinatario.ShowDialog()

        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim n1 As New DataEnvio

            Dim TraerDatos As IEnvio = DirectCast(n1, IEnvio)
            Dim fecha1 As Date = "01/01/10"
            Dim fecha2 As Date = "06/03/10"

            Dim Datos As DataSet = TraerDatos.TraerEnviosPorFechas(fecha1, fecha2)
            DataGridViewEnvios.DataSource = Datos.Tables(0)
        End Sub




        Sub EditarEnvio(ByVal sender As System.Object, ByVal e As System.EventArgs)



            IdEnvio = DataGridViewEnvios.CurrentRow.Cells(0).Value
            MsgBox(IdEnvio)

            Dim n3 As New DataEnvio
            Dim TraerEnvio As IEnvio = DirectCast(n3, IEnvio)
            Dim Envio1 As Envio = TraerEnvio.TraerEnvio(IdEnvio)

            IdEnvio = Envio1.IdEnvio
            Fecha = Envio1.Fecha
            Remitente = Envio1.Remitente
            Destinatario = Envio1.Destinatario
            Movil = Envio1.Movil
            TipoDePago = Envio1.TipoDePago
            TipoDeEnvio = Envio1.TipoDeEnvio
            ImporteEnvio = Envio1.ImporteEnvio
            ImporteContrareembolso = Envio1.ImporteContrareembolso
            CantidadDePaquetes = Envio1.CantidadDePaquetes
            UltimoEstado = Envio1.UltimoEstado


            If TipoDeEnvio = 1 Then RadioButtonExpreso.Checked = True
            If TipoDeEnvio = 2 Then RadioButtonContrareembolso.Checked = True

            If TipoDePago = 1 Then RadioButtonPagoOrigen.Checked = True
            If TipoDePago = 2 Then RadioButtonPagoDestino.Checked = True


            
            TextBoxRemito.Text = IdEnvio
            DateTimePickerFecha.Value = Fecha
            TextBoxOrigenID.Text = Remitente
            TextBoxDestinoID.Text = Destinatario
            ComboBoxMovil.SelectedValue = Movil
            TextBoxImporteEnvio.Text = ImporteEnvio
            TextBoxImporteContrareembolso.Text = ImporteContrareembolso


            TraerEnviosaGrid()



            'TxtCodigoFamiliar.Text = DataGridViewFamiliar.CurrentRow.Cells(0).Value
            'TxtNombreFamiliar.Text = DataGridViewFamiliar.CurrentRow.Cells(1).Value
            'txtDniFamiliar.Text = DataGridViewFamiliar.CurrentRow.Cells(2).Value
            'DTPFechaNacimientoFamiliar.Value = DataGridViewFamiliar.CurrentRow.Cells(3).Value
            'DataGridViewFamiliar.Rows.RemoveAt(DataGridViewFamiliar.CurrentRow.Index)
        End Sub


        Sub BorrarEnvio(ByVal sender As System.Object, ByVal e As System.EventArgs)

            Dim a As Integer

            a = DataGridViewEnvios.CurrentRow.Cells(0).Value
          
            Dim n3 As New DataEnvio

            Dim Borrar As IEnvio = DirectCast(n3, IEnvio)
            Dim Resultado As Boolean = Borrar.BorrarEnvio(a)

            TraerEnviosaGrid()

        End Sub



        Private Sub TraerMovilesTodos()

            Dim Moviles As IMovil = DirectCast(New DataMovil, IMovil)
            Dim Resultado As DataSet = Moviles.TraerMovilesTodos()


            With ComboBoxMovil
                .DataSource = Resultado.Tables(0)
                .DisplayMember = "Descripcion"
                .ValueMember = "IDMovil"
            End With


            For Each Dr In Resultado.Tables(0).Rows
                ' ComboBoxMovil.Items.Add(Dr(0))
            Next

            ComboBoxMovil.SelectedIndex = 0

          
        End Sub



        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

            If RadioButtonExpreso.Checked = True Then TipoDeEnvio = 1
            If RadioButtonContrareembolso.Checked = True Then TipoDeEnvio = 2

            If RadioButtonPagoOrigen.Checked = True Then TipoDePago = 1
            If RadioButtonPagoDestino.Checked = True Then TipoDePago = 2

            Dim n As New DataEnvio
            Dim m As New DataHistoriaEnvio


            Try
                n.IdEnvio = TextBoxRemito.Text
                n.Fecha = DateTimePickerFecha.Value
                n.Remitente = TextBoxOrigenID.Text
                n.Destinatario = TextBoxDestinoID.Text
                n.Movil = ComboBoxMovil.SelectedValue.ToString
                n.TipoDePago = TipoDePago
                n.TipoDeEnvio = TipoDeEnvio
                n.ImporteEnvio = CDec(TextBoxImporteEnvio.Text)
                n.ImporteContrareembolso = CDec(TextBoxImporteContrareembolso.Text)
                n.CantidadDePaquetes = 0
                n.UltimoEstado = 2

                m.IDEnvio = n.IdEnvio
                m.Fecha = Now.Date
                m.Hora = Now.TimeOfDay
                m.Estado = 2
                m.Posicion = "OFICINA CENTRAL"

            Catch ex As Exception

                MsgBox(ex.Message)
                MsgBox(m.Hora)

            End Try

            Dim GrabarE As IEnvio = DirectCast(New DataEnvio, IEnvio)
            Dim ResultadoE As Boolean = GrabarE.AgregarEnvio(n)

            Dim GrabarH As IHistoriaEnvio = DirectCast(New DataHistoriaEnvio, IHistoriaEnvio)
            Dim ResultadoH As Boolean = GrabarH.AgregarHistoria(m)

            TraerEnviosaGrid()
            InicializarFormulario()


        End Sub


        Private Sub TextBoxRemito_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBoxRemito.LostFocus

            If IsNumeric(TextBoxRemito.Text) And TextBoxRemito.Text.Length > 0 Then
                TextBoxRemito.Enabled = False
            Else
                MsgBox("El Remito tiene que ser un valor valido")
                TextBoxRemito.Text = ""
                TextBoxRemito.Focus()

            End If

        End Sub


        Private Sub MaskedTextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.LostFocus

            If Not IsDate(MaskedTextBox1.Text) Then
                MsgBox("Fecha No Valida")
                MaskedTextBox1.Focus()
                MaskedTextBox1.SelectAll()
            End If

        End Sub

        Private Sub GroupBox4_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox4.Enter

        End Sub

        Private Sub DateTimePickerFecha_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerFecha.ValueChanged
            MaskedTextBox1.Text = Format(DateTimePickerFecha.Value.Date, "ddMMyy")

        End Sub

        Private Sub DataGridViewEnvios_DoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridViewEnvios.CellContentClick

            If e.ColumnIndex() = 1 Then

                Dim FormularioPaquetes As New FrmPaquetes
                FormularioPaquetes.TextBoxEnvioPaquete.Text = DataGridViewEnvios.Rows(e.RowIndex).Cells(0).Value
                ValorSeleccionado = Val(DataGridViewEnvios.Rows(e.RowIndex).Cells(0).Value)
                FormularioPaquetes.ShowDialog()

            End If

        End Sub




        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

            Dim n1 As New DataEnvio
            Dim TraerDatos As IEnvio = DirectCast(n1, IEnvio)
            Dim Ds As DataSet = TraerDatos.TraerEnviosCompletosPorFechas("01/01/10", "31/12/10", 2)

            Dim Reporte1 As New FrmReporteEnvios
            Dim Rpt As New CrystalReport2


            Reporte1.CrystalReportViewer1.ReportSource = Rpt
            Rpt.SetDatabaseLogon("sa2", 1234)
            Rpt.SetDataSource(Ds)
            Reporte1.Show()


        End Sub

        Private Sub RadioButtonContrareembolso_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButtonContrareembolso.CheckedChanged
            If RadioButtonContrareembolso.Checked Then
                TextBoxImporteContrareembolso.Enabled = True
            Else
                TextBoxImporteContrareembolso.Enabled = False
                TextBoxImporteContrareembolso.Text = "0"
            End If
        End Sub


        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

            InicializarFormulario()
            TextBoxRemito.Focus()

        End Sub


        Public Sub InicializarFormulario()

            TextBoxRemito.Text = ""
            TextBoxRemito.Enabled = True
            Me.LabelOrigenNombre.Text = ""
            Me.LabelOrigenDomicilio.Text = ""
            Me.LabelOrigenCUIT.Text = ""
            Me.LabelOrigenLocalidad.Text = ""
            Me.LabelOrigenCP.Text = ""
            Me.TextBoxOrigenID.Text = ""

            Me.LabelDestino.Text = ""
            Me.LabelDestinoDomicilio.Text = ""
            Me.LabelDestinoCUIT.Text = ""
            Me.LabelDestinoLocalidad.Text = ""
            Me.LabelDestinoCP.Text = ""
            Me.TextBoxDestinoID.Text = ""

            DateTimePickerFecha.Value = Now.Date

            TextBoxImporteContrareembolso.Text = ""
            TextBoxImporteContrareembolso.Enabled = False
            TextBoxImporteEnvio.Text = ""

            RadioButtonExpreso.Checked = True
            RadioButtonPagoOrigen.Checked = True

        End Sub


        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

            If RadioButtonExpreso.Checked = True Then TipoDeEnvio = 1
            If RadioButtonContrareembolso.Checked = True Then TipoDeEnvio = 2

            If RadioButtonPagoOrigen.Checked = True Then TipoDePago = 1
            If RadioButtonPagoDestino.Checked = True Then TipoDePago = 2

            Dim n As New DataEnvio
            Dim EnvioModificar As New Envio

            EnvioModificar.IdEnvio = TextBoxRemito.Text
            EnvioModificar.Fecha = DateTimePickerFecha.Value
            EnvioModificar.Remitente = TextBoxOrigenID.Text
            EnvioModificar.Destinatario = TextBoxDestinoID.Text
            EnvioModificar.Movil = ComboBoxMovil.SelectedValue.ToString
            EnvioModificar.TipoDePago = TipoDePago
            EnvioModificar.TipoDeEnvio = TipoDeEnvio
            EnvioModificar.ImporteEnvio = CDec(TextBoxImporteEnvio.Text)
            EnvioModificar.ImporteContrareembolso = CDec(TextBoxImporteContrareembolso.Text)
            EnvioModificar.CantidadDePaquetes = 0
            EnvioModificar.UltimoEstado = 2

            MsgBox(IdEnvio)

            Dim ModificarEnvio As IEnvio = DirectCast(n, IEnvio)
            Dim ResultadoModificar As Boolean = ModificarEnvio.Modificar(IdEnvio, EnvioModificar)

            TraerEnviosaGrid()
            InicializarFormulario()

        End Sub
    End Class

End Namespace